package com.zteqhd.plugins;

import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Build;
import android.widget.ImageView;

import com.cesards.cropimageview.CropImageView;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.BufferedSink;

/**
 * Created by jiachengwang on 2017/6/14.
 */

public class zteQhdPlugin extends CordovaPlugin {

    private final int SEL_PHOTO_IMAGE=999006;

    private final int CROP_PHOTO_IMAGE=999005;

    private CallbackContext callbackContext;

    public boolean execute(String action, JSONArray args,
                           final CallbackContext callbackContext) throws JSONException {

        if (action.equals("uploadFiles")) {

            String url = args.getString(0);
            String filedir = args.getString(1);

            Map<String, Object> params = new HashMap<String, Object>();

            params.put("file", new File(filedir));
            params.put("type",args.getString(3));
            params.put("path",args.getString(4));

            try {
                zteQhdHttpService.uploadFile(url, params, callbackContext);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }else if(action.equals("getImage")){
            this.callbackContext=callbackContext;
            Intent intent = new Intent();
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            intent.setType("image/*");
//根据版本号不同使用不同的Action
            if (Build.VERSION.SDK_INT <19) {
                intent.setAction(Intent.ACTION_GET_CONTENT);
            } else {
                intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
            }
            this.cordova.getActivity().startActivityForResult(intent, SEL_PHOTO_IMAGE);
        }else if(action.equals("cropImage")){

            


        }

        return true;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {

        if(requestCode == SEL_PHOTO_IMAGE){

            Uri uri = intent.getData();

            PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, uri.toString());

            this.callbackContext.sendPluginResult(pluginResult);
        }


    }


}
